<?php
namespace Home\Controller;

class UsercenterController extends AdminController{

	private $_userPhoto;//操作相册对像
	private $_userInfo;//操作用户信息的表
    private $_user;//操作用户表
    private $_model; //操作model类
    private $_foucs; //操作 love_foucs类
	public function __construct(){
		parent::__construct();
		$this->_user=M('user');
        $this->_userInfo=M('userinfo');
		$this->_userPhoto=M('userphoto');
		$this->_model=M();
		$this->_foucs=M('foucs');
	}
	public function index(){
		$id=$_SESSION['love_user']['id'];
       /* var_dump($_SESSION);*/
        $tu = M('userphoto')->where("uid=$id and is_face = 1")->select();
        var_dump($tu);
         //模拟session
        $sql="select s_city,sex from love_userinfo where uid={$id}";
        $dataCity=$this->_userInfo->query($sql);
        // dump($dataCity);    
        $cityId=(empty($dataCity[0]['s_city'])==1)?6:$dataCity[0]['s_city'];
        $sex=$dataCity[0]['SEX'];
        $tsex=($id=='1')?1:0; 
        // dump($tsex);
        $sql="select u.uid uuid,u.name uname,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city={$cityId} and p.is_face=1 and sex={$tsex} and u.uid in (select kid from love_foucs where uid={$id}) order by rand() limit 5";
        $dataList1=$this->_model->query($sql);
        // dump($this->_model->getLastSql());
        //查询未读消息
        
        //查询已关注的
        $sql1="select count(id) as num from love_foucs where uid={$id}";
        $ygz=$this->_foucs->query($sql1);
        
        //查询积分
   		 $sql2="select integral from love_userinfo where uid={$id}";
         $num=$this->_model->query($sql2);
         	 dump($num);
        //查询被关注的
        $sql3="select count(id) as mum from love_foucs where kid={$id}";
        $bgz=$this->_foucs->query($sql3);

        $this->assign('ygz',$ygz);
        $this->assign('num',$num);
        $this->assign('bgz',$bgz);
        $this->assign('dataList',$dataList1);
        $this->assign('tu',$tu);

		$this->display();
	}
	public function upload(){

		/*dump(__ROOT__);
        dump(I('get.id/d'));*/

		$upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();  
        $fileName='C:/wamp/www/lover/object/app/Uploads'.'/'.$info['avatar_file']['savepath'];
 
        $imgName=$info['avatar_file']['savename'];
       
        import("Org.Util.picture");
        $imgDeal=new \picture($fileName.$imgName);
        $code=$_POST['avatar_data'];
        $code_deal=json_decode($code);
    
        $temp=$code_deal->x;
        $imgDeal->CUT_X=$code_deal->x;
        $imgDeal->CUT_Y=$code_deal->y;
        $imgDeal->CUT_WIDTH=$code_deal->width;
        $imgDeal->CUT_HEIGHT=$code_deal->height;
        $imgDeal->CUT();

         imagejpeg($imgDeal->TRUE_COLOR,$fileName.$temp.$imgName);   ///输出图片 此处待修改
     
         if(!$info) {// 上传错误提示错误信息
             $this->error($upload->getError());
             }else{// 上传成功
        	//将图片信息存入数据库
		       $img['name']=__ROOT__.'/Uploads/'.$info['avatar_file']['savepath'].$temp.$imgName;
		       $img['uid']=I('get.id/d');
		       $img['is_face']=1;
		       /*var_dump($img);*/
		       $uid=$img['uid'];
		       // var_dump($uid);
		       $this->_userPhoto->is_face=0;
		       $this->_userPhoto->where("uid={$uid}")->save();
		       $this->_userPhoto->add($img);
		      }
	}

    public function money(){
        /*var_dump($_SESSION);*/
        $uid = $_SESSION['love_user']['id'];
        $money = $_POST['value'];
        /*var_dump($_POST);
        var_dump($money);*/
        /*exit;*/

        if($_SESSION['dict']==0){
            $x = 100;
        }else{
             $x = $_SESSION['dict'];  
        }
        $integral = $money * $x ;
        $ral['uid'] = $uid;
        /*var_dump($ral['uid']);
        exit;*/
        $ral['type'] = 1; 
        $ral['value'] = $integral;
        $ral['reason'] = '充值';
        $ral['utime'] = time();
        $data = M('userinfo')->where("uid=$uid")->find();
        /*var_dump($data);*/
        /*echo M('userinfo')->getLastSql();*/
        /*exit;*/
        /*var_dump($data['integral']);
        var_dump($integral);
        exit;*/
        $ral1 = $data['integral']+$integral;
        /*echo $ral;*/
        M('userinfo')->integral = $ral1;
        if(M('userinfo')->where("uid=$uid")->save()>0){
            /*echo M('userinfo')->getLastSql();
            exit;*/
            if(M('integral')->add($ral)>0){
               /* echo M('integral')->getLastSql();
                exit;*/
                $this->success('充值成功',U('Usercenter/index'));
            }else{
                /*echo 1;*/
                $this->error('充值失败');
            }
        }else{
            /*echo M('userinfo')->getLastSql();
            exit;
            echo 2;*/
            $this->error('充值失败');
        }

    }

    public function gift(){
        $id=$_SESSION['love_user']['id'];
       /* var_dump($_SESSION);*/
        $tu = M('userphoto')->where("uid=$id and is_face = 1")->select();
        var_dump($tu);
         //模拟session
        $sql="select s_city,sex from love_userinfo where uid={$id}";
        $dataCity=$this->_userInfo->query($sql);
        // dump($dataCity);    
        $cityId=(empty($dataCity[0]['s_city'])==1)?6:$dataCity[0]['s_city'];
        $sex=$dataCity[0]['SEX'];
        $tsex=($id=='1')?1:0; 
        // dump($tsex);
        $sql="select u.uid uuid,u.name uname,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city={$cityId} and p.is_face=1 and sex={$tsex} and u.uid in (select kid from love_foucs where uid={$id}) order by rand() limit 5";
        $dataList1=$this->_model->query($sql);
        // dump($this->_model->getLastSql());
        //查询未读消息
        
        //查询已关注的
        $sql1="select count(id) as num from love_foucs where uid={$id}";
        $ygz=$this->_foucs->query($sql1);
        
        //查询积分
         $sql2="select integral from love_userinfo where uid={$id}";
         $num=$this->_model->query($sql2);
          dump($num);
        //查询被关注的
        $sql3="select count(id) as mum from love_foucs where kid={$id}";
        $bgz=$this->_foucs->query($sql3);

        $this->assign('ygz',$ygz);
        $this->assign('num',$num);
        $this->assign('bgz',$bgz);
        $this->assign('dataList',$dataList1);

        $this->assign('mtu',$tu);


        /*var_dump($_SESSION);*/
        $uid = $_SESSION['love_user']['id'];
       $data1 = M('dealgift')->where("uid=$uid")->select();
       $data2 = M('dealgift')->where("eid=$uid")->select();
      /*var_dump($data1);*/
      for($i=0;$i<count($data1);$i++){
        $uid1 = $data1[$i]['eid'];
        $gid = $data1[$i]['gid'];
        $data3 = M('gift')->where("id=$gid")->select();
        $data11 = M('user')->where("id=$uid1")->select();

       /* var_dump($data3);*/
        $data1[$i]['name'] = $data3[0]['name'];
        $data1[$i]['gift_name'] = $data3[0]['gift_name'];
        $data1[$i]['value'] = $data3[0]['value'];
        $data1[$i]['user_name'] = $data11[0]['name'];

      }
      

     for($j=0;$j<count($data2);$j++){
        $gid = $data2[$j]['gid'];
        $uid2 = $data2[$j]['uid'];
       /* var_dump($gid);
        exit;*/
        $data4 = M('gift')->where("id=$gid")->select();
        $data12 = M('user')->where("id=$uid2")->select();
        /*var_dump($data3);*/
        $data2[$j]['name'] = $data4[0]['name'];
        $data2[$j]['gift_name'] = $data4[0]['gift_name'];
        $data2[$j]['value'] = $data4[0]['value'];
        $data2[$j]['user_name'] = $data12[0]['name'];
      }


      $this->assign('list1',$data1); 
      $this->assign('list2',$data2);
      $this->display();
    }
    // 打招呼函数
    public function message(){
        $id=$_SESSION['love_user']['id'];
       /* var_dump($_SESSION);*/
        $tu = M('userphoto')->where("uid=$id and is_face = 1")->select();
        // var_dump($tu);
         //模拟session
        $sql="select s_city,sex from love_userinfo where uid={$id}";
        $dataCity=$this->_userInfo->query($sql);
        // dump($dataCity);    
        $cityId=(empty($dataCity[0]['s_city'])==1)?6:$dataCity[0]['s_city'];
        $sex=$dataCity[0]['SEX'];
        $tsex=($id=='1')?1:0; 
        // dump($tsex);
        $sql="select u.uid uuid,u.name uname,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city={$cityId} and p.is_face=1 and sex={$tsex} and u.uid in (select kid from love_foucs where uid={$id}) order by rand() limit 5";
        $dataList1=$this->_model->query($sql);
        // dump($this->_model->getLastSql());
        //查询未读消息
        
        //查询已关注的
        $sql1="select count(id) as num from love_foucs where uid={$id}";
        $ygz=$this->_foucs->query($sql1);
        
        //查询积分
         $sql2="select integral from love_userinfo where uid={$id}";
         $num=$this->_model->query($sql2);
             // dump($num);
        //查询被关注的
        $sql3="select count(id) as mum from love_foucs where kid={$id}";
        $bgz=$this->_foucs->query($sql3);

        $this->assign('ygz',$ygz);
        $this->assign('num',$num);
        $this->assign('bgz',$bgz);
        $this->assign('dataList',$dataList1);
        $this->assign('tu',$tu);
        // 查询发送消息
        $callSql="select  eid,time,content from love_email where uid={$id} and type=0";
         $callData=$this->_model->query($callSql);
         dump($this->_model->getLastSql());
         for($j=0;$j<count($callData);$j++){
            $eid =$callData[$j]['eid'];
             $data12 = M('user')->where("id=$eid")->select();
            $callData[$j]['name'] = $data12[0]['name'];
         }
        dump($callData);
    //查询我收到的
         $callSql_T="select  eid,time,content from love_email where eid={$id} and type=0";
         $callData_T=$this->_model->query($callSql_T);
         // dump($this->_model->getLastSql());
         for($j=0;$j<count($callData_T);$j++){
            $eid =$callData_T[$j]['eid'];
             $data12 = M('user')->where("id=$eid")->select();
            $callData_T[$j]['name'] = $data12[0]['name'];
         }
        $this->assign('callData',$callData);
        $this->assign('callData_T',$callData_T);
        $this->display();
    }
    //邮件函数
     public function email(){
        $id=$_SESSION['love_user']['id'];
       /* var_dump($_SESSION);*/
        $tu = M('userphoto')->where("uid=$id and is_face = 1")->select();
        // var_dump($tu);
         //模拟session
        $sql="select s_city,sex from love_userinfo where uid={$id}";
        $dataCity=$this->_userInfo->query($sql);
        // dump($dataCity);    
        $cityId=(empty($dataCity[0]['s_city'])==1)?6:$dataCity[0]['s_city'];
        $sex=$dataCity[0]['SEX'];
        $tsex=($id=='1')?1:0; 
        // dump($tsex);
        $sql="select u.uid uuid,u.name uname,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city={$cityId} and p.is_face=1 and sex={$tsex} and u.uid in (select kid from love_foucs where uid={$id}) order by rand() limit 5";
        $dataList1=$this->_model->query($sql);
        // dump($this->_model->getLastSql());
        //查询未读消息
        
        //查询已关注的
        $sql1="select count(id) as num from love_foucs where uid={$id}";
        $ygz=$this->_foucs->query($sql1);
        
        //查询积分
        $sql2="select integral from love_userinfo where uid={$id}";
         $num=$this->_model->query($sql2);

         // dump($num);
        //查询被关注的
        $sql3="select count(id) as mum from love_foucs where kid={$id}";
        $bgz=$this->_foucs->query($sql3);

        $this->assign('ygz',$ygz);
        $this->assign('num',$num);
        $this->assign('bgz',$bgz);
        $this->assign('dataList',$dataList1);
        $this->assign('tu',$tu);


        // 查询发送邮件
        $callSql="select  eid,time,content from love_email where uid={$id} and type=1";
         $callData=$this->_model->query($callSql);
         dump($this->_model->getLastSql());
         for($j=0;$j<count($callData);$j++){
            $eid =$callData[$j]['eid'];
             $data12 = M('user')->where("id=$eid")->select();
            $callData[$j]['name'] = $data12[0]['name'];
         }
        dump($callData);
      //查询我收到的
         $callSql_T="select  eid,time,content from love_email where eid={$id} and type=1";
         $callData_T=$this->_model->query($callSql_T);
         // dump($this->_model->getLastSql());
         for($j=0;$j<count($callData_T);$j++){
            $eid =$callData_T[$j]['eid'];
             $data12 = M('user')->where("id=$eid")->select();
            $callData_T[$j]['name'] = $data12[0]['name'];
         }
        $this->assign('callData',$callData);
        $this->assign('callData_T',$callData_T);
        $this->display();
    } 
    //我的关注函数

    public function foucsPeople(){
        $id=$_SESSION['love_user']['id'];
        var_dump($_SESSION);
        $tu = M('userphoto')->where("uid=$id and is_face = 1")->select();
        // var_dump($tu);
         //模拟session
        $sql="select s_city,sex from love_userinfo where uid={$id}";
        $dataCity=$this->_userInfo->query($sql);
        // dump($dataCity);    
        $cityId=(empty($dataCity[0]['s_city'])==1)?6:$dataCity[0]['s_city'];
        $sex=$dataCity[0]['SEX'];
        $tsex=($id=='1')?1:0; 
        // dump($tsex);
        $sql="select u.uid uuid,u.name uname,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city={$cityId} and p.is_face=1 and sex={$tsex} and u.uid in (select kid from love_foucs where uid={$id}) order by rand() limit 5";
        $dataList1=$this->_model->query($sql);
        // dump($this->_model->getLastSql());
        //查询未读消息
        
        //查询已关注的
        $sql1="select count(id) as num from love_foucs where uid={$id}";
        $ygz=$this->_foucs->query($sql1);
        
        //查询积分
        $sql2="select integral from love_userinfo where uid={$id}";
         $num=$this->_model->query($sql2);

         // dump($num);
        //查询被关注的
        $sql3="select count(id) as mum from love_foucs where kid={$id}";
        $bgz=$this->_foucs->query($sql3);

        $this->assign('ygz',$ygz);
        $this->assign('num',$num);
        $this->assign('bgz',$bgz);
        $this->assign('dataList',$dataList1);
        $this->assign('tu',$tu);
       
        // 查询我的关注
         $callSql="select kid from love_foucs where uid={$id}";
         $callData=$this->_model->query($callSql);
         dump($callData);

         for($j=0;$j<count($callData);$j++){
            $kid =$callData[$j]['kid'];
            $data12 = M('userinfo')->where("uid=$kid")->select();
            $callData[$j]['name'] = $data12[0]['name'];
            $callData[$j]['sex'] = $data12[0]['sex'];
            $data13=M('userphoto')->where("uid={$kid} and is_face=1")->select();
            $callData[$j]['path'] = $data12[0]['name'];
         }
        //查询谁关注我
         $callSql_T="select uid kid from love_foucs where kid={$id}";
         $callData_T=$this->_model->query($callSql_T);

         for($j=0;$j<count($callData_T);$j++){
            $kid =$callData_T[$j]['kid'];
            $data12 = M('userinfo')->where("uid=$kid")->select();
            $callData_T[$j]['name'] = $data12[0]['name'];
            $callData_T[$j]['sex'] = $data12[0]['sex'];
            $data13=M('userphoto')->where("uid={$kid} and is_face=1")->select();
            $callData_T[$j]['path'] = $data12[0]['name'];
         }

        $this->assign('callData',$callData);
        $this->assign('callData_T',$callData_T);
         


        $this->display();
    } 


}
